//
//  HomeTableViewController.swift
//  ZMWWeibo
//
//  Created by 张美文 on 16/1/4.
//  Copyright © 2016年 walter. All rights reserved.
//

import UIKit

class HomeTableViewController: BaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //1.如果没有登录，就设置未登录界面信息
        if !userLogin
        {
            visitorView?.setupVisitorInfo(true, imageName: "visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        //2.初始化导航条
        setupNav()

    }
    
    private func setupNav()
    {
        /*
        //1. 左边按钮
        let leftBtn = UIButton()
        leftBtn.setImage(UIImage(named: "navigationbar_friendattention"), forState: UIControlState.Normal)
        leftBtn.sizeToFit()
        leftBtn.setImage(UIImage(named: "navigationbar_friendattention_highlighted"), forState: UIControlState.Highlighted)
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftBtn)
        
        //2. 右边按钮
        let rightBtn = UIButton()
        rightBtn.setImage(UIImage(named: "navigationbar_pop"), forState: UIControlState.Normal)
        rightBtn.sizeToFit()
        rightBtn.setImage(UIImage(named: "navigationbar_pop_highlighted"), forState: UIControlState.Highlighted)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBtn)
        */
        
        /*
        navigationItem.leftBarButtonItem = createBarButtonItem("navigationbar_friendattention", target: self, action: "leftItemClick")
        navigationItem.rightBarButtonItem = createBarButtonItem("navigationbar_pop", target: self, action: "rightItemClick")
        */
        
        //1. 初始化左右按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem.createBarButtonItem("navigationbar_friendattention", target: self, action: "leftItemClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem.createBarButtonItem("navigationbar_pop", target: self, action: "rightItemClick")
        
        //初始化标题按钮
        let titleBtn = TitleButton()
        titleBtn.setTitle("walter  ", forState: UIControlState.Normal)
        titleBtn.addTarget(self, action: "titleBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        navigationItem.titleView = titleBtn
    }
    
    func titleBtnClick(btn: UIButton)
    {
        //1. 修改箭头的方向
        btn.selected = !btn.selected
        
        //2. 弹出菜单
        let sb = UIStoryboard(name: "PopoverViewController", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        //2.1 设置转场代理
        vc?.transitioningDelegate = self
        //2.2 设置转场样式
        vc?.modalPresentationStyle = UIModalPresentationStyle.Custom
        presentViewController(vc!, animated: true, completion: nil)
    }
    
    func leftItemClick()
    {
        print(__FUNCTION__)
    }
    
    func rightItemClick()
    {
        print(__FUNCTION__)
    }
    
    /*
    private func createBarButtonItem(imageName:String, target:AnyObject?, action:Selector) -> UIBarButtonItem
    {
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.sizeToFit()
        btn.setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        return UIBarButtonItem(customView: btn)
    }
    */

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }
    
    /// 记录当前是否展开
    var isPresnt: Bool = false
}

extension HomeTableViewController: UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning
{
    
    //实现代理方法，告诉系统谁来负责转场动画
    //UIPresentationController是iOS8中专门用于负责转场动画的
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?
    {
        
        return PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
    }
    
    // MARK: - 只要实现了一下方法，系统默认的动画都没有了，要自己手动实现

    /**
     告诉系统谁来负责modal的展现动画
     
     - parameter presented:  被展现的视图
     - parameter presenting: 发起的视图
     - parameter source:     source description
     
     - returns: 谁来负责
     */
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isPresnt = true
        return self
    }
    
    /**
     告诉系统谁来负责modal的消失动画
     
     - parameter dismissed: 被关闭的视图
     
     - returns: 谁来负责
     */
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isPresnt = false
        return self
    }
    
    //MARK: - UIViewControllerAnimatedTransitioning
    /**
    返回动画时长
    
    - parameter transitionContext: 上下文，里面保存动画需要的所有参赛
    
    - returns: 动画时长
    */
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval
    {
        return 0.5
    }
    
    /**
     告诉系统如何动画，无论是展示还是消失都会调用这个方法
     
     - parameter transitionContext: 上下文，里面保存了动画需要的所有参数
     */
    func animateTransition(transitionContext: UIViewControllerContextTransitioning)
    {
        //1. 拿到展现的视图
        /*
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
        print(toVC)
        print(fromVC)
        */
        
        if isPresnt
        {
            //展开
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            toView.transform = CGAffineTransformMakeScale(1.0, 0.0)
            
            // 注意：一定要将视图添加到容器上
            transitionContext.containerView()?.addSubview(toView)
            
            //设置锚点
            toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            //2. 执行动画
            UIView .animateWithDuration(0.5, animations: { () -> Void in
                //2.1 清空transform
                toView.transform = CGAffineTransformIdentity
                }) { (_) -> Void in
                    //2.2 动画执行完毕，一定要告诉系统
                    //如果不写，可能导致一些未知的错误
                    transitionContext.completeTransition(true)
            }
        }else
        {
            //关闭
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
            UIView .animateWithDuration(0.2, animations: { () -> Void in
                //注意：CGFloat是不准确的，所以如果写0.0会没有动画
                fromView?.transform = CGAffineTransformMakeScale(1.0, 0.000001)
                }, completion: { (_) -> Void in
                    //
                    transitionContext.completeTransition(true)
            })
        }
        
        
        
    }
}









































